package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.MemberCardService;
import com.junzhiit.nengji.cms.web.exception.MemberCardNotFoundException;
import com.junzhiit.nengji.cms.web.vo.MemberCardVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.ValueScopeVO;
import com.junzhiit.nengji.dao.CouponsDAO;
import com.junzhiit.nengji.dao.MemberCardDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.MemberCard;
import com.junzhiit.nengji.model.enums.MemberCardStatus;
import com.junzhiit.nengji.model.enums.MemberCardType;
import com.junzhiit.nengji.model.enums.MemberCardValidTime;

@Service
public class MemberCardServiceImpl extends BaseServiceImpl implements MemberCardService {

	@Autowired
	private MemberCardDAO memberCardDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private CouponsDAO couponsDAO;
	
	@Override
	public ResultVO findAll(MemberCardVO memberCardVO) {
		ResultVO result = new ResultVO();
		Map<Integer,Object> map=new HashMap<Integer,Object>();
		List<MemberCard> generalMemberCard = new ArrayList<MemberCard>();
		List<MemberCard> ruleMemberCard = new ArrayList<MemberCard>();
		List<MemberCard> paymentMemberCard = new ArrayList<MemberCard>();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(memberCardVO.getSessionId()), SystemUserSessionVO.class);
		
		List<MemberCard> memberCard=memberCardDAO.findAll(systemUserSessionVO.getSystemId());
		
		for(int i=0;i<memberCard.size();i++){
			if(memberCard.get(i).getType() == MemberCardType.ZERO_LEVEL.toIntValue()){
				generalMemberCard.add(memberCard.get(i));
			}else if(memberCard.get(i).getType() == MemberCardType.ONE_LEVEL.toIntValue()){
				ruleMemberCard.add(memberCard.get(i));
			}else if(memberCard.get(i).getType() == MemberCardType.TWO_LEVEL.toIntValue()){
				paymentMemberCard.add(memberCard.get(i));
			}
		}
		map.put(MemberCardType.ZERO_LEVEL.toIntValue(), generalMemberCard);
		map.put(MemberCardType.ONE_LEVEL.toIntValue(), ruleMemberCard);
		map.put(MemberCardType.TWO_LEVEL.toIntValue(), paymentMemberCard);
		result.setData(map);
		return result;
	}

	@Override
	public ResultVO add(MemberCardVO memberCardVO) {
		ResultVO result = new ResultVO();
		Date now=new Date();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(memberCardVO.getSessionId()), SystemUserSessionVO.class);
		
		MemberCard memberCard=new MemberCard();
		BeanUtils.copyProperties(memberCardVO, memberCard);
		
		memberCard.setCsId(systemUserSessionVO.getSystemId());
		memberCard.setState(1);
		memberCard.setOpenTime(now);
		if(memberCard.getType() == MemberCardType.ONE_LEVEL.toIntValue()){
			memberCard.setCardGrade(Integer.valueOf(memberCardVO.getCardGrade()));
		}else{
			memberCard.setCardGrade(null);
		}
		
		//设置会员等级
//		if(memberCard.getGrade() == null){
//			memberCard.setGrade(MemberLevelCode.ZERO.toIntValue());
//		}
		
		memberCardDAO.insert(memberCard);
		return result;
	}

	@Override
	public ResultVO findMemberCardById(String cardId) {
		ResultVO result=new ResultVO();
		MemberCardVO memberCardVO=new MemberCardVO();
		MemberCard memberCard=memberCardDAO.query(cardId);
		if(memberCard==null){
			throw new MemberCardNotFoundException("找不到该会员卡");
		}
		BeanUtils.copyProperties(memberCard, memberCardVO);
		if(StringUtils.isNotBlank(memberCard.getCouponsId())){
			memberCardVO.setCouponsName(couponsDAO.findById(memberCard.getCouponsId()).getName());
		}
		result.setData(memberCardVO);
		return result;
	}

	@Override
	public ResultVO updateMemberCard(MemberCardVO memberCardVO) {
		ResultVO result=new ResultVO();
		MemberCard memberCard=memberCardDAO.query(memberCardVO.getId());
		if(memberCard==null){
			throw new MemberCardNotFoundException("找不到该会员卡");
		}
		BeanUtils.copyProperties(memberCardVO, memberCard);
		memberCardDAO.update(memberCard,true);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		MemberCard memberCard = memberCardDAO.query(id);
		if (memberCard == null) {
			throw new MemberCardNotFoundException("找不到该会员卡");
		}
		boolean data = memberCardDAO.updateToDelete(id);
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO findMemberCardNameAll(MemberCardVO memberCardVO) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(memberCardVO.getSessionId()), SystemUserSessionVO.class);
		
		result.setData(memberCardDAO.findMemberCardName(systemUserSessionVO.getSystemId()));
		
		return result;
	}

	@Override
	public void updateMemberCardStatus() {
		Date now = new Date();
		List<MemberCard> memberCard = memberCardDAO.findActiveCard();
		for(MemberCard card:memberCard){
			if(card.getValidTimeState() == MemberCardValidTime.FIXED.toIntStatus() && card.getValidEndTime().getTime() <= now.getTime()){
				card.setState(MemberCardStatus.OVERDUE.toIntStatus());
				memberCardDAO.update(card);
			}
		}
	}

	@Override
	public ResultVO getMaxGrade(String sessionId) {
		ResultVO result = new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		MemberCard memberCard = memberCardDAO.getMaxGrade(systemUserSessionVO.getSystemId());
		if(memberCard == null){
			MemberCard card = new MemberCard();
			card.setBuyCount(0);
			card.setCardGrade(0);
			card.setConsumePrice(0.00);
			card.setConsumeIntegral(0);
			result.setData(card);
		}else{
			result.setData(memberCard);
		}
		return result;
	}

	@Override
	public ResultVO getScopeValue(String sessionId, String id) {
		ResultVO result = new ResultVO();
		int min=1;
		Double minPrice=0.01;
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String shopId = systemUserSessionVO.getSystemId();
		MemberCard memberCard = memberCardDAO.query(id);
		ValueScopeVO valueScopeVO = new ValueScopeVO();
		
		MemberCard minBuyCount = memberCardDAO.getMinBuyCount(memberCard.getBuyCount(),shopId);
		if(minBuyCount != null){
			valueScopeVO.setMinBuyCount(minBuyCount.getBuyCount()+min);
		}else{
			valueScopeVO.setMinBuyCount(min);
		}
		
		MemberCard maxBuyCount = memberCardDAO.getMaxBuyCount(memberCard.getBuyCount(),shopId);
		if(maxBuyCount != null){
			valueScopeVO.setMaxBuyCount(maxBuyCount.getBuyCount()-min);
		}else{
			valueScopeVO.setMaxBuyCount(null);
		}
		
		MemberCard minConsumePrice = memberCardDAO.getMinConsumePrice(memberCard.getConsumePrice(), shopId);
		if(minConsumePrice != null){
			valueScopeVO.setMinConsumePrice(minConsumePrice.getConsumePrice()+minPrice);
		}else{
			valueScopeVO.setMinConsumePrice(minPrice);
		}
		
		MemberCard maxConsumePrice = memberCardDAO.getMaxConsumePrice(memberCard.getConsumePrice(), shopId);
		if(maxConsumePrice != null){
			valueScopeVO.setMaxConsumePrice(maxConsumePrice.getConsumePrice()-minPrice);
		}else{
			valueScopeVO.setMaxConsumePrice(null);
		}
		
//		MemberCard minConsumeIntegral = memberCardDAO.getMinConsumeIntegral(memberCard.getConsumeIntegral(), shopId);
//		if(minConsumeIntegral != null){
//			valueScopeVO.setMinConsumeIntegral(minConsumeIntegral.getConsumeIntegral()+min);
//		}else{
//			valueScopeVO.setMinConsumeIntegral(min);
//		}
//		
//		MemberCard maxConsumeIntegral = memberCardDAO.getMaxConsumeIntegral(memberCard.getConsumeIntegral(), shopId);
//		if(maxConsumeIntegral != null){
//			valueScopeVO.setMaxConsumeIntegral(maxConsumeIntegral.getConsumeIntegral()-min);
//		}else{
//			valueScopeVO.setMaxConsumeIntegral(null);
//		}
		
		result.setData(valueScopeVO);
		return result;
	}
}
